/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.h2psoft.truyen.tranh.activities;

import vn.persoft.extra.customview.SlidingMenu;
import vn.persoft.extra.customview.SlidingMenu.OnClosedListener;
import vn.persoft.extra.customview.SlidingMenu.OnOpenedListener;
import vn.persoft.extra.dialog.ConfirmDialog;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;

import com.h2psoft.truyen.tranh.R;
import com.h2psoft.truyen.tranh.adapter.AdapterListFragment;
import com.h2psoft.truyen.tranh.constants.Constants;
import com.h2psoft.truyen.tranh.dialog.LoadingDialog;
import com.h2psoft.truyen.tranh.slidingmenu.BaseActivity;

public class MainActivity extends BaseActivity {
	private String tag = MainActivity.class.getCanonicalName();

	private AdapterListFragment fragmentAdapter;
	private Display display;
	private int version = android.os.Build.VERSION.SDK_INT;
	public static ViewPager mPager;
	public static LoadingDialog loadingDialog;
	private PagerTabStrip pagerTabStrip;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		defineValues();
		initPage();
		initMenu();
	}

	private void defineValues() {
		fragmentAdapter = new AdapterListFragment(getSupportFragmentManager(), this);
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setOffscreenPageLimit(1);
		pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
		pagerTabStrip.setDrawFullUnderline(false);
		loadingDialog = new LoadingDialog(this);
		loadingDialog.setCancelable(false);
		loadingDialog.setCanceledOnTouchOutside(false);
		Constants.WIDTH = getWidthScreen();
		Log.e(tag, "Width size: " + Constants.WIDTH);
	}

	private void initPage() {
		mPager.setAdapter(fragmentAdapter);
		mPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				if (arg0 == 0) {
					if (FragmentListComic.btnNew != null)
						FragmentListComic.btnNew.setChecked(false);
					getSlidingMenu().removeIgnoredView(mPager);
				} else {
					if (FragmentListComic.btnNew != null)
						FragmentListComic.btnNew.setChecked(true);
					getSlidingMenu().addIgnoredView(mPager);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				switch (arg0) {
				case 0:
					getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
					break;
				default:
					getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
					break;
				}
			}
		});
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
	}

	private void initMenu() {
		getSlidingMenu().setOnClosedListener(new OnClosedListener() {
			@Override
			public void onClosed() {
			}
		});
		getSlidingMenu().setOnOpenedListener(new OnOpenedListener() {

			@Override
			public void onOpened() {
			}
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			if (FragmentListComic.btnMenu.isChecked() == true) {
				getSlidingMenu().showContent();
				FragmentListComic.btnMenu.setChecked(false);
			} else
				getSlidingMenu().showMenu();
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			if (FragmentListComic.btnNew.isChecked() == true) {
				mPager.setCurrentItem(0, true);
				FragmentListComic.btnNew.setChecked(false);
				return true;
			} else if (FragmentListComic.btnMenu.isChecked() == true) {
				getSlidingMenu().showContent();
				FragmentListComic.btnMenu.setChecked(false);
				return true;
			} else {
				ConfirmDialog confirmDialog = new ConfirmDialog(this, new ConfirmDialog.ReadyListener() {

					@Override
					public void onOk() {
						finish();
					}

					@Override
					public void onNo() {

					}

					@Override
					public void onClose() {

					}
				}, R.string.exitTitle, R.string.exitTitle, false);
				confirmDialog.show();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);

	}

	private int getWidthScreen() {
		int width = 0;
		display = getWindowManager().getDefaultDisplay();
		if (version >= 13) {
			Point size = new Point();
			display.getSize(size);
			width = size.x;

		} else {
			display = getWindowManager().getDefaultDisplay();
			width = display.getWidth();
		}
		width -= (width / 3);
		return width;
	}

	public void onBackClick(View v) {

	}

	public void onNextClick(View v) {

	}
}